﻿using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using HorizontalAlignment = System.Windows.HorizontalAlignment;
using MessageBox = System.Windows.Forms.MessageBox;


namespace SellAvi.Classes
{
    /// <summary>
    /// TODO: Унифицировать класс и сделать его пригодным для использования ControlCaptcha
    /// </summary>
    public class InputBox
    {

        Window Box = new Window();//window for the inputbox
        //FontFamily font = new FontFamily("Tahoma");//font for the whole inputbox
        //int FontSize = 30;//fontsize for the input
        StackPanel sp1 = new StackPanel();// items container
        string title = "Окно ручного ввода";//title as heading
        string boxcontent;//title
        string defaulttext = "";//default textbox caption
        string errormessage = "Неверный формат ссылки";//error messagebox caption
        string errortitle = "Errore";//error messagebox heading title
        string okbuttontext = "OK";//Ok button caption
        TextBox input = new TextBox();
        Button ok = new Button();


        public InputBox(string caption)
        {

            boxcontent = caption;
            WindowInit();
        }
        public InputBox(string caption, string windowTitle, string DefaultText)
        {

            boxcontent = caption;
            title = windowTitle;
            defaulttext = DefaultText;
            WindowInit();
        }
        public InputBox(string caption, string windowTitle)
        {
            boxcontent = caption;
            title = windowTitle;
            WindowInit();
        }
        private void WindowInit()// window building - check only for window size
        {
            Box.Height = 120;// Box Height
            Box.Width = 400;// Box Width
            Box.Title = title;
            Box.Content = sp1;
            //Box.Closing += Box_Closing;
            TextBlock captionTextBlock = new TextBlock();
            captionTextBlock.TextWrapping = TextWrapping.Wrap;
            captionTextBlock.Background = null;
            captionTextBlock.HorizontalAlignment = HorizontalAlignment.Center;
            captionTextBlock.Text = boxcontent;

            sp1.Children.Add(captionTextBlock);

            input.HorizontalAlignment = HorizontalAlignment.Center;
            input.Text = defaulttext;
            input.MinWidth = 200;
            input.MouseEnter += ClearTextBox;
            input.KeyDown += textBox_KeyDown;
            sp1.Children.Add(input);

            ok.Width = 70;
            ok.Height = 20;
            ok.Click += (sender, args) => Box.Close();
            ok.Content = okbuttontext;
            ok.HorizontalAlignment = HorizontalAlignment.Center;
            sp1.Children.Add(ok);

            input.Focus();

        }

        private void ClearTextBox(object sender, MouseEventArgs e)
        {
            if ((sender as TextBox).Text == defaulttext)
            {
                (sender as TextBox).Text = null;
            }
        }

        public string ShowDialog()
        {
            Box.ShowDialog();
            return input.Text;
        }

        private void textBox_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter || e.Key == Key.Escape)
            {
                Box.Close();
            }

        }


    }
}
